package com.firefly.service.command.cms;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.firefly.dao.dao.cms.po.AdPosPo;
import com.firefly.dao.dao.cms.po.AdPo;
import com.firefly.dao.dao.cms.service.AdService;
import com.firefly.util.handle.FireflyException;
import com.firefly.dao.assembler.cms.AdPosAssembler;
import com.firefly.dao.dto.CommonDto;
import com.firefly.dao.dto.cms.AdPosDto;
import com.firefly.service.repository.cms.AdPosRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @description: 广告位
 * @author: lzg(萤火科技 ： 854378082 @ qq.com)
 * @date: 2022/12/18
 **/
@Service
public class AdPosCommand {
    private final AdPosAssembler adPosAssembler;
    private final AdPosRepository adPosRepository;
    private final AdService adsService;
    @Autowired
    public AdPosCommand(AdPosAssembler adPosAssembler, AdPosRepository adPosRepository, AdService adsService){
        this.adPosAssembler = adPosAssembler;
        this.adPosRepository = adPosRepository;
        this.adsService = adsService;
    }
    public Boolean create(AdPosDto request) {
        AdPosPo entity = adPosAssembler.getPo(request);
        return adPosRepository.create(entity);
    }
    public Boolean update(AdPosDto request) {
        AdPosPo entity = adPosAssembler.getPo(request);
        return adPosRepository.update(entity);
    }
    public Boolean removeByIds(CommonDto request) {
        QueryWrapper<AdPo> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("pos_id",request.getDetailIds()).eq("del_flag",0);
        if (adsService.count(queryWrapper) > 0){
            throw new FireflyException(403,"广告位下有广告，请先删除广告！");
        }
        return adPosRepository.updateDelFlagByIds(request.getDetailIds());
    }
}
